package memorphic.slide.plugin.wizards;

import java.io.BufferedReader;
import java.io.FileReader;
import java.net.URL;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * @author masi
 *
 */
public class CreateSlideView extends WizardPage {
	public IProject project;
	
	private Text viewName;
	private Text viewPackage;
	private Button createDelegate;
	private Text delegateName;
	private Text delegatePackage;
	
//	private ISelection selection;
	
	// ---------------------------------------------------------------------------
	//
	// Constructor
	//
	// ---------------------------------------------------------------------------

	/**
	 * Constructor for GenerateSlideWizardPage.
	 * 
	 * @param selection
	 */
	public CreateSlideView(ISelection selection) {
		super("wizardPage");
		setTitle("Define Slide project structure");
		setDescription("This wizard generates a slide framework for a given flex project.");
//		this.selection = selection;
		
	}
	
	public void createControl(Composite parent) {

		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;
		layout.verticalSpacing = 9;

		Label label = new Label(container, SWT.NULL);
		label.setText("&View:");
		viewName = new Text(container, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		viewName.setLayoutData(gd);
		viewName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged(e);
			}
		});

		label = new Label(container, SWT.NULL);
		label.setText("&View Package:");
		viewPackage = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		viewPackage.setLayoutData(gd);
		viewPackage.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged(e);
			}
		});

		label = new Label(container, SWT.NULL);
		label.setText("&Create Presentation Model:");
		
		createDelegate = new Button(container, SWT.CHECK);
		createDelegate.setSelection(true);
		createDelegate.addSelectionListener (new SelectionAdapter() {
		 	public void widgetSelected(SelectionEvent e) {
		 		checkBoxSelecionChanged();
		 	}
		 });

		label = new Label(container, SWT.NULL);
		label.setText("&Presentation Model:");
		delegateName = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		delegateName.setLayoutData(gd);
		delegateName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged(e);
			}
		});

		label = new Label(container, SWT.NULL);
		label.setText("&Presentation Model package:");
		delegatePackage = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		delegatePackage.setLayoutData(gd);
		delegatePackage.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged(e);
			}
		});
		
		initialize();
		setControl(container);
	}
	
	// ---------------------------------------------------------------------------
	//
	// Private Methods
	//
	// ---------------------------------------------------------------------------
	
	/**
	 * Tests if the current workbench selection is a suitable container to use.
	 */

	private void initialize() {
		String packageID = getPackage() + ".views";
		viewPackage.setText(packageID);
		delegatePackage.setText(packageID);
	}
	
	public String getPackage() {
		String packageID = "";
		try {
			URL u = project.getFile(".slideProject").getLocationURI().toURL();
			URL fileURL = FileLocator.toFileURL(u);
			BufferedReader in = new BufferedReader(new FileReader(fileURL.getPath()));
			String s;
			while ((s = in.readLine()) != null)
			{
				String[] parts = s.split(":");
				if (parts[0].equals("packageID"))
				{
					packageID = parts[1];
					break;
				}
			}
		}
		catch (Exception e) {
			// TODO: handle exception
		}
		
		return packageID;
	}
	
	private String getAppPropsInterface() {
		String iAppProps = "";
		try {
			URL u = project.getFile(".slideProject").getLocationURI().toURL();
			URL fileURL = FileLocator.toFileURL(u);
			BufferedReader in = new BufferedReader(new FileReader(fileURL.getPath()));
			String s;
			while ((s = in.readLine()) != null)
			{
				String[] parts = s.split(":");
				if (parts[0].equals("iAppProps"))
				{
					iAppProps = parts[1];
					break;
				}
			}
		}
		catch (Exception e) {
			// TODO: handle exception
		}
		
		return iAppProps;
	}
	
	public String getBaseViewName() {
		String baseView = "";
		try {
			URL u = project.getFile(".slideProject").getLocationURI().toURL();
			URL fileURL = FileLocator.toFileURL(u);
			BufferedReader in = new BufferedReader(new FileReader(fileURL.getPath()));
			String s;
			while ((s = in.readLine()) != null)
			{
				String[] parts = s.split(":");
				if (parts[0].equals("baseView"))
				{
					String fileName = parts[1];
					baseView = fileName.substring(0, fileName.length() - 3);
					break;
				}
			}
		}
		catch (Exception e) {
			// TODO: handle exception
		}
		
		return baseView;
	}
	
	private void dialogChanged(ModifyEvent e) {
		if (e.widget == viewName)
		{
			if (viewName.getText().equals(""))
			{
				delegateName.setText("");
				updateStatus("View name must be specified.");
				return;
			} else
			{
				delegateName.setText(viewName.getText() + "PresModel");
			}
		}
		if (e.widget == viewPackage)
		{
			delegatePackage.setText(viewPackage.getText());
			if (viewPackage.getText().equals(""))
			{
				delegateName.setText("");
				updateStatus("Package must be specified.");
				return;
			}
		}
		if (e.widget == delegateName)
		{
			if (createDelegate.getSelection() && delegateName.getText().equals(""))
			{
				updateStatus("A name for the delegate must be specified.");
				return;
			}
		}
		if (e.widget == delegatePackage)
		{
			if (createDelegate.getSelection() && delegatePackage.getText().equals(""))
			{
				updateStatus("A package for the delegate must be specified.");
				return;
			}
		}
		
		updateStatus(null);
	}
	
	private void checkBoxSelecionChanged() {
		if (createDelegate.getSelection())
		{
			delegateName.setText(viewName.getText() + "Delegate");
			delegatePackage.setText(viewPackage.getText());
		}
		else
		{
			delegateName.setText("");
			delegatePackage.setText("");
		}
		delegateName.setEnabled(createDelegate.getSelection());
		delegatePackage.setEnabled(createDelegate.getSelection());
	}

	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}
	
	public String getViewName() {
		return viewName.getText();
	}
	
	public String getViewPackage() {
		return viewPackage.getText();
	}

	public String getDelegateName() {
		return delegateName.getText();
	}

	public String getDelegatePackage() {
		return delegatePackage.getText();
	}
	
	public String getSourceDirectory() {
		return project.getFolder("src").getLocation().toOSString();
	}
	
	public Boolean getCreateDelegate() {
		return createDelegate.getSelection();
	}
	
	public String getIAppProps() {
		return getAppPropsInterface();
	}
	
}
